package com.supermap.dataservice.cloud.cust.role;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.cloud.cust.menu.TreeNode;
import com.supermap.dubbo.api.cloud.cust.role.SysAreaRoleService;
import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author aqua
 * @desc
 * @date 2019年02月28 17:15
 * @email 1301584033@qq.com
 */
@Service
public class SysAreaRoleServiceImpl implements SysAreaRoleService {
    @Resource
    private SysAreaRoleMapper sysAreaRoleMapper;

    @Override
    public JSONUtil getTreeArea() {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        TreeNode treeNode=new TreeNode();
        treeNode.setId(0);
        treeNode.setCode("0");
        treeNode.setName("全部节点");
        treeNode.setOpen(true);
        List<TreeNode> treeNodes=sysAreaRoleMapper.getAllArea();
        treeNodes.add(treeNode);
        return  new JSONUtil().setData(treeNodes);
    }

    @Override
    public JSONUtil getTreeRole(String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        TreeNode treeNode=new TreeNode();
        treeNode.setId(0);
        treeNode.setCode("0");
        treeNode.setName("全部节点");
        treeNode.setOpen(true);
        List<TreeNode> treeNodes=sysAreaRoleMapper.getAllRole(areaCode);
        for(TreeNode treeNode1:treeNodes){
            if(treeNode1.getCategory()==1){
                treeNode1.setChecked(true);
            }
        }
        treeNodes.add(treeNode);
        return  new JSONUtil().setData(treeNodes);
    }

    @Override
    public JSONUtil putAreaRole(String areaCode, String roleId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        sysAreaRoleMapper.delAreaRole(areaCode);
        for (String str:roleId.split(",")){
            sysAreaRoleMapper.putAreaRole(areaCode,str);
        }
        return new JSONUtil();
    }

    @Override
    public JSONUtil getTreeAreaRole(String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtil().setData(sysAreaRoleMapper.getAllAreaRoleByRoleId(areaCode));
    }

    @Override
    public JSONUtil getTreeAreaRoleMenu(String areaCode, Integer roleId,Integer userId,Integer state) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        String menids="";
        if(state==1){
            //区域菜单
            menids=sysAreaRoleMapper.getAreaCodeMenidS(areaCode);
        }else if(state==2){
            //角色菜单
            menids=sysAreaRoleMapper.getAreaCodeRoleMenidS(areaCode,roleId);
        }else {
            //用户菜单
            menids=sysAreaRoleMapper.getAreaCodeRoleUserMenidS(userId);
        }
        List<TreeNodeList> sysAreaMenus= sysAreaRoleMapper.getTreeAreaRoleMenu(areaCode,roleId,menids,3,null,state);
        for(TreeNodeList nodeList:sysAreaMenus){
            nodeList.setChildren(getAreaMenuList(nodeList,areaCode,roleId,menids,state));
        }

        return new JSONUtil().setData(sysAreaMenus);

    }



    public List<TreeNodeList> getAreaMenuList(TreeNodeList sysMenu,String areaCode,Integer roleId,String menids,Integer state){
        List<TreeNodeList> sysMenus = new ArrayList<>();
        if(sysMenu.getCategory()!=1){
            return  sysMenus;
        }
        List<TreeNodeList>  sysMenuList=sysAreaRoleMapper.getTreeAreaRoleMenu(areaCode,roleId,menids,sysMenu.getCode().length()+3,sysMenu.getCode(),state);
        for(TreeNodeList sysMenu1:sysMenuList) {
            if (sysMenu1 != null) {
                sysMenu1.setChildren(getAreaMenuList(sysMenu1,areaCode,roleId,menids,state));
                sysMenus.add(sysMenu1);
            }
        }
        return sysMenus;
    }

    @Override
    public JSONUtil putRoleAreaMenu(String menuId, Integer areaRoleId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        sysAreaRoleMapper.putRoleAreaMenu(menuId,areaRoleId);
        return new JSONUtil();
    }


}
